package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.constants.SystemConstants;
import com.doubi.mapper.SysUserMapper;
import com.doubi.model.sys.TSysUser;
import com.doubi.utils.DataUtil;
import com.doubi.utils.MD5Util;
import com.doubi.utils.SecuritySHA1Utils;
import com.doubi.vo.AppModel;
import com.doubi.vo.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class SysUserService extends ServiceImpl<SysUserMapper,TSysUser> {

    @Autowired
    private SysUserMapper sysUserMapper;


    public void updateUserToUserType(String userOid,String userType){
        TSysUser sysUser = sysUserMapper.selectById(userOid);
        sysUser.setUserType(userType);
        sysUserMapper.updateById(sysUser);
    }

    @Async("taskExecutor")
    public void updateUserLastTime(String userOid){
        UpdateWrapper<TSysUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("oid",userOid);
        wrapper.set("last_login_time",new Date());
        super.update(wrapper);
    }

    /**
     * MrDoubi
     * 2019年10月20日 21:17:50
     * 根据账号获取用户信息
     * @param account 账号
     * @return model
     */
    public TSysUser getTSysUserByAccountAdmin(String account){
        QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("user_type", SystemConstants.UserType.ADMIN);
        return super.getOne(queryWrapper);
    }

    /**
     * MrDoubi
     * 2019年10月20日 21:17:50
     * 根据账号获取用户信息
     * @param account 账号
     * @return model
     */
    public TSysUser getTSysUserByAccount(String account){
        QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account);
        return super.getOne(queryWrapper);
    }


    /**
     * 修改用户资料
     * zhangqy 2019年4月4日16:03:37
     * @param sysUser 当前用户
     * @param user 用户修改得值
     * @return model
     */
    public TSysUser updateUserDate(TSysUser sysUser, TSysUser user){
        sysUser.setAccount(user.getAccount());
        sysUser.setHead(user.getHead());
        sysUser.setName(user.getName());
        sysUserMapper.updateById(sysUser);
        return sysUser;
    }

    /**
     * 功能描述：修改用户密码
     * @author   MrDoubi 2019-06-27 09:41
     * @param sysUser           用户对象
     * @param newPwd            新密码
     * @return                  返回用户对象
     */
    public TSysUser updatePwd(TSysUser sysUser, String newPwd) throws Exception {
        sysUser.setPassword(MD5Util.md5Encode(newPwd));
        sysUserMapper.updateById(sysUser);
        return sysUser;
    }
    /**
     * 功能描述：app修改用户密码
     * @author   MrDoubi 2019-06-27 09:41
     * @param sysUser           用户对象
     * @param newPwd            新密码
     */
    public void updateUiPwd(TSysUser sysUser, String newPwd){
        sysUser.setPassword(newPwd);
        sysUserMapper.updateById(sysUser);
    }


    /**
     * 功能说明：分页用户列表
     * @author  MrDoubi 2019-06-28 09:05:33
     * @param page page
     * @param sysUser bean
     * @return page
     */
    public PageVo<TSysUser> getPage(PageVo<TSysUser> page, TSysUser sysUser) {
        return sysUserMapper.selectPageVo(page,sysUser);
    }

    /**
     * 功能说明：用户删除
     * @author  MrDoubi 2019-01-10 15:19:14
     * @param sysUser bean
     * @return model
     */
    public AppModel delBean(TSysUser sysUser) {
        sysUserMapper.deleteById(sysUser.getOid());
        return new AppModel(0,"操作成功！");
    }


    public AppModel updateResetPassword(String oid){
        TSysUser sysUser = sysUserMapper.selectById(oid);
        if(sysUser.getUserType().equals(SystemConstants.UserType.ADMIN)){
            sysUser.setPassword(MD5Util.generate("123456"));
        }else {
            try {
                sysUser.setPassword(SecuritySHA1Utils.shaEncode("123456"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        sysUserMapper.updateById(sysUser);
        return AppModel.success();
    }
    public AppModel updateAble(String oid){
        TSysUser sysUser = sysUserMapper.selectById(oid);
        if(sysUser.getIsAble().equals(SystemConstants.YESORNO.YES)){
            sysUser.setIsAble(SystemConstants.YESORNO.NO);
        }else {
            sysUser.setIsAble(SystemConstants.YESORNO.YES);
        }
        sysUserMapper.updateById(sysUser);
        return AppModel.success();
    }


    /**
     * MrDoubi
     * 2020年4月1日 13:55:52
     * home 的统计
     */
    public AppModel getHomeStatistics(){
        Map<String,Double> map = new HashMap<>();
        map.put("todaysCount", Double.valueOf(sysUserMapper.getCountByTodays()));
        map.put("toMonthCount", Double.valueOf(sysUserMapper.getCountByToMonth()));
        Integer allCount = sysUserMapper.getAllCount();
        map.put("allCount", Double.valueOf(allCount));
        Integer allServiceCount = sysUserMapper.getAllCountByService();
        map.put("allServiceCount", Double.valueOf(allServiceCount));
        map.put("allCountDays7", (double) sysUserMapper.getAllCountByDays7() / allCount);
        map.put("allServiceCountDays7", (double) sysUserMapper.getAllCountByServiceDays7() / allServiceCount);
        return AppModel.success(map);
    }

}
